﻿#include "CrystalSpider.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirEnvir/Envir.h"
#include "../MapObject.h"
#include "../../MirEnvir/Map.h"
#include "../../Settings.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    CrystalSpider::CrystalSpider(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool CrystalSpider::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 3 || y > 3)
        {
            return false;
        }

        return (x == 0) || (y == 0) || (x == y);
    }

    void CrystalSpider::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        if (!ranged)
        {
            MonsterObject::Attack();
        }
        else
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->Type = 1;
            Broadcast(tempVar);
            LineAttack(3, 300, DefenceType::MACAgility);

            ActionTime = getEnvir()->getTime() + 300;
            AttackTime = getEnvir()->getTime() + AttackSpeed;
            ShockTime = 0;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void CrystalSpider::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);
        bool attack = data.size() < 4 || std::any_cast<bool>(data[3]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        if (!attack || target->Attacked(this, damage, defence) <= 0)
        {
            return;
        }

        PoisonTarget(target, 8, 5, PoisonType::Green, 2000);
    }

    void CrystalSpider::LineAttack(int distance, int additionalDelay, DefenceType defenceType, bool push)
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        for (int i = 1; i <= distance; i++)
        {
            Point *target = Functions::PointMove(getCurrentLocation(), getDirection(), i);

            if (!getCurrentMap()->ValidPoint(target))
            {
                continue;
            }

            Cell *cell = getCurrentMap()->GetCell(target);
            if (cell->Objects.empty())
            {
                continue;
            }

            for (int o = 0; o < cell->Objects.size(); o++)
            {
                MapObject *ob = cell->Objects[o];
                if (ob->getRace() == ObjectType::Monster || ob->getRace() == ObjectType::Player)
                {
                    if (!ob->IsAttackTarget(this))
                    {
                        continue;
                    }

                    if (getEnvir()->Random->Next(Settings::MagicResistWeight) >= ob->Stats[Stat::MagicResist])
                    {
                        int delay = Functions::MaxDistance(getCurrentLocation(), ob->getCurrentLocation()) * 50 + additionalDelay; //50 MS per Step
                        DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + delay, {ob, damage, defenceType, true});
                        ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
                    }
                }
                else
                {
                    continue;
                }

                break;
            }
        }
    }
}
